import {
	HTTP_REQUEST_URL,
	TOKENNAME,
	STOREAGE
} from "@/config/app"
import {
	checkLogin
} from "@/config/login"

/**
 * 发送请求
 */
export const request = (url, method, data = {}, auth = false) => {
	let token = uni.getStorageSync(STOREAGE.TOKEN)
	let header = {
		'content-type': 'application/json',
		'Authori-zation': 'Bearer ' + token
	}

	if (auth === false) {
		let is_login = checkLogin()
		if (is_login === false) {
			uni.navigateTo({
				url: "/pages/login/login"
			})
			return false
		}
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: HTTP_REQUEST_URL + url,
			method: method,
			header: header,
			data: data,
			success(res) {
				resolve(res.data)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}